from fastapi import APIRouter, HTTPException
from .models import OutRoom, OutColor, InRoom, InColor
from .sub_utils import BlobControl
import asyncio
import time
import re

router = APIRouter(
    prefix="/BlobControl",
    tags=["灯泡控制"]
)

dbBlob = BlobControl()


@router.post("/Color", response_model=OutColor)
async def blobColor(color: InColor):
    if re.search('#[a-fA-F0-9]{6}', color.color) is None:
        raise HTTPException(status_code=412, detail='颜色字符串非法!')
    await dbBlob.setColorCode(color.color)
    return {'color': dbBlob.getColorCode(), 'time': time.time() * 1000}


@router.post("/Room", response_model=OutRoom)
async def blobRoom(room: InRoom):
    if room.room not in ['客厅', '走廊', '卧室']:
        raise HTTPException(status_code=412, detail='房间不存在!')
    dbBlob.setRoomName(room.room)
    return {'room': dbBlob.getRoomName(), 'time': time.time() * 1000}
